//
//  ViewImage.m
//  weibo3
//
//  Created by Tian Wu on 11-4-16.
//  Copyright 2011年 nfsysu. All rights reserved.
//

#import "ViewImage.h"
#import "User.h"


@implementation ViewImage

@synthesize thisStatus;

- (NSString *)windowNibName
{
	return @"ViewImage";
}


- (IBAction) cancel:(id) sender{
    
    [NSApp stopModal];
}


- (IBAction) didRetweet:(id) sender{
    
    if(!newTweetWindow){
        newTweetWindow = [[newTweet alloc] init];
    }
    
    [newTweetWindow edit:self.thisStatus from:self];
    
}
- (IBAction) didCommon:(id) sender{
    
    if(!commentWindow){
        commentWindow = [[commentViewController alloc] init];
    }
    
    [commentWindow edit:self.thisStatus from:self];
    
}

- (void) alertEnded:(NSAlert *) alert code:(int) choice context:(void *) v{
    if (choice == NSAlertDefaultReturn) {
        [_rootViewController statusDestroy:[self.thisStatus aid]];
        [NSApp stopModal];
    }
}

- (IBAction) didDelet:(id) sender{
    
    NSAlert *alert = [NSAlert alertWithMessageText:@"确定要删除吗？" defaultButton:@"确定" alternateButton:@"取消" otherButton:nil informativeTextWithFormat:@"微博删除之后将无法恢复！"];
    [alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(alertEnded:code:context:) contextInfo:NULL];
}

- (IBAction) didDM:(id) sender{
    
    /*if(!newDMWindow){
        newDMWindow = [[newDM alloc] init];
    }
    
    [newDMWindow edit:self.thisStatus from:self];*/
}


- (NSString *) shortCount:(int) count{
    
    NSString *shorted = [NSString stringWithFormat:@"%d",count];
    
    if (count > 99999) {
        shorted = [NSString stringWithFormat:@"%d万",count/10000];
    }
    
    return shorted;
}


- (BOOL)edit:(Status *)startingValues from:(id)sender
{
	NSWindow *selfwindow = [self window];
    
    
    //DButton title setting
    
    User *currentUser = [_rootViewController verifyUser];
    
    if( [currentUser uid] == [[startingValues user] uid]){
        
        [dButton setTitle:@"删除"];
        [dButton setAction:@selector(didDelet:)];
        
    }
    else{
        
        [dButton setTitle:@"私信"];
        [dButton setAction:@selector(didDM:)];
        
    }
    
    // content size setting start
    
    NSString *staText = [startingValues text];
    if ([startingValues retweeted_status] != NULL) {
        staText = [staText stringByAppendingFormat:@"\n----------------\n@%@:%@",[[[startingValues retweeted_status] user] name],[[startingValues retweeted_status] text]];
    }
    
    NSTextStorage *textStorage = [[[NSTextStorage alloc] initWithString:staText] autorelease];
    NSTextContainer *textContainer = [[[NSTextContainer alloc]
                                       initWithContainerSize: NSMakeSize(355, FLT_MAX)] autorelease];
    NSLayoutManager *layoutManager = [[[NSLayoutManager alloc] init]
                                      autorelease];
    [layoutManager addTextContainer:textContainer];
    [textStorage addLayoutManager:layoutManager];
    [textStorage addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Lucida Grande" size:12.0]
                        range:NSMakeRange(0, [textStorage length])];
    [textContainer setLineFragmentPadding:6.0];
    (void) [layoutManager glyphRangeForTextContainer:textContainer];
    
    [selfwindow setContentSize:NSMakeSize(497, [layoutManager usedRectForTextContainer:textContainer].size.height + 405)];
    
    // content size setting end
    
    self.thisStatus = startingValues;
    [name setTitleWithMnemonic:[[startingValues user] screen_name]];
    NSImage *image = [[NSImage alloc] initWithContentsOfURL:[NSURL URLWithString:[[startingValues user] profile_image_url]]];
    [img setImageScaling:NSScaleToFit];
    [img setImage:image];
    [image release];
    
    
    [fans setTitleWithMnemonic:[NSString stringWithFormat:@"关注:%@  粉丝:%@  微博:%@",[self shortCount:[[startingValues user] friends_count]],[self shortCount:[[startingValues user] followers_count]],[self shortCount:[[startingValues user] statuses_count]]]];
    
    [descrip setTitleWithMnemonic:[[startingValues user] description]];
    
    if([startingValues retweeted_status] != NULL){
        [text setTitleWithMnemonic:[NSString stringWithFormat:@"%@\n----------------\n@%@:%@",[startingValues text],[[[startingValues retweeted_status] user] name],[[startingValues retweeted_status] text]]];
    }
    else{
        [text setTitleWithMnemonic:[startingValues text]]; 
    }
    /*
    [text setTextContainer:textContainer];
    [text setAutomaticLinkDetectionEnabled:YES];
    */
    
    if([[startingValues user] following]){
        [isfollowing setTitleWithMnemonic:@"已关注"];
    }
    else{
        [isfollowing setTitleWithMnemonic:@"未关注"];
    }
    
    
    [imageView setDrawsBackground:NO];
    
    if([startingValues thumbnail_pic] == nil){
        
        if([[startingValues retweeted_status] thumbnail_pic] != nil){
            
            
            [[imageView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[[startingValues retweeted_status] bmiddle_pic]]]];
            
        }
        else{
            
            [[imageView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]]];
            
        }
    }
    else{
            
            [[imageView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[startingValues bmiddle_pic]]]];
            
    }
        
        [NSApp beginSheet:selfwindow modalForWindow:[[NSApplication sharedApplication] mainWindow] modalDelegate:nil didEndSelector:nil contextInfo:nil];
        [NSApp runModalForWindow:selfwindow];
        // sheet is up here...
        
        [NSApp endSheet:selfwindow];
        [selfwindow orderOut:self];
    
    return YES;
}



- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        
    }
    
    return self;
}



- (void)dealloc
{
    [super dealloc];
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

@end
